<?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        include_once('ns_include.php');
        
        //Creating new software
        if (isset($_POST["createSoftware"]) && $_POST["createSoftware"] == "Save")
        {            
            $result = ns_dao_software::CreateNewSoftwareDescription($_POST['Name'], mysql_real_escape_string($_POST['Description']), $_POST['Category'], $_POST['Owner'],$_POST['Approved']);
        }
        
        //Updating the software
        if (isset($_POST["saveDescription"]) && $_POST["saveDescription"] == "Save")
        {
            $result = ns_dao_software::updateSoftware($_POST['softwareID'], "SoftwareName", $_POST['Name']);
            $result = ns_dao_software::updateSoftware($_POST['softwareID'], "SoftwareDescription", $_POST['Description']);
            $result = ns_dao_software::updateSoftware($_POST['softwareID'], "Category", $_POST['Category']);
            $result = ns_dao_software::updateSoftware($_POST['softwareID'], "OwnerUsername", $_POST['Owner']);
            $result = ns_dao_software::updateSoftware($_POST['softwareID'], "Approved", $_POST['Approved']);
            header("Location: manage_content.php?id={$_POST['softwareID']}");
        }
        
        //Creating a new software version
        if (isset($_POST["createVersion"]) && $_POST["createVersion"] == "Save")
        {
            $result = ns_dao_software::CreateSoftwareVersion($_POST['softwareID'], $_POST['newVersionNum']);
            header("Location: manage_content.php?id=".$_POST['softwareID']."&view=manageVersion");
            //$versionData = ns_dao_software::SoftwareVersionLatestBySoftwareID($_POST['softwareID']);
            //$versionData->UploadFile($_FILES["versionFile"]);
            //header("Location: manage_content.php?id={$_POST['softwareID']}");
        }
        
        //Creating a new software version
        if (isset($_POST["uploadFile"]) && $_POST["uploadFile"] == "Upload")
        {
            $versionData = ns_dao_software::SoftwareVersionLatestBySoftwareID2($_POST['softwareID']);
            //$versionData->UploadFile($_FILES["versionFile"]);
            //header("Location: manage_content.php?id=".$_POST['softwareID']."&view=manageVersion&version=".$version);
            
            $postedFile = $_FILES['versionFile'];
            $path_parts = pathinfo($postedFile["name"]);
            $ext = $path_parts['extension'];
            
            $newPathName = "../../content/software/".$versionData[0]["SoftwareID"]."/".$versionData[0]["SoftwareVersionID"]."/".basename($postedFile['name']);           
            @move_uploaded_file($postedFile['tmp_name'], $newPathName);
            header("Location: manage_content.php?id=".$_POST['softwareID']."&view=manageVersion&version=".$version);
        }
        
        //For updating the software avatar
        if (isset($_POST["uploadAvatar"]) && $_POST["uploadAvatar"] == "Update Avatar")
        {
            $messageString = "Avatar updated";          
            $softwareData = ns_dao_software::SoftwareByID($_POST['softwareID']);
            $result = $softwareData->UpdateAvatarImage($_FILES["avatarFile"]);
            if (!$result)
            {
                $messageString = "Avatar was not updated";
            }
            
            /*
            $path_parts = pathinfo($postedFile["name"]);
            $ext = $path_parts['extension'];
            $newFilename = $this->ID() . "-avatar." . $ext;
            $newPathName = self::AvatarImagePath($newFilename);

            @move_uploaded_file($postedFile['tmp_name'], $newPathName);
            if (!file::reallyExists($newPathName)) return FALSE;

            $updated = ns_dao_software::updateSoftware($this->ID(), "AvatarImage", "");
            $updated = ns_dao_software::updateSoftware($this->ID(), "AvatarImage", $newFilename);

            $this->AvatarImage($newFilename);
            return $updated;*/
        }
        
        //Delete software
        if (isset($_POST["delete"]) && $_POST["delete"] == "Delete")
        {
            $result = ns_dao_software::DeleteSoftware($_POST['softwareID']);
        }
        
        //Delete version
        if (isset($_POST["deleteVersion"]) && $_POST["deleteVersion"] == "Delete")
        {
            $result = ns_dao_software::DeleteSoftwareVersion($_POST['softwareVersionID']);
        }
    }
?>
